﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using System.Windows.Forms;
using System.Collections.Generic;
using System;
using CoreShop.PrintService;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.App
{
    public partial class frmPrintTemplateInfo : UIForm
    {
        corecmsprintbilltemplate template = new corecmsprintbilltemplate();
        public frmPrintTemplateInfo(corecmsprintbilltemplate _template)
        {
            InitializeComponent();
            template = _template;
        }

        public frmPrintTemplateInfo()
        {
        }

        private void frmPrintSchemeInfo_Load(object sender, System.EventArgs e)
        {
            BindComboBoxData();
            ControlsDataBinding.DataBind(template, this, DataSourceUpdateMode.OnPropertyChanged);
            try
            {
                //打印类型
                corecmsprintbilltype type = new corecmsprintbilltypeServices().SelectByType(template.billtypeId);
                if (type != null) billtypeId.SelectedValue = type.id;
            }
            catch { }
        }

        public void BindComboBoxData()
        {
            try
            {
                #region 加载打印类型
                this.billtypeId.Items.Clear();
                List<corecmsprintbilltype> typeList = new corecmsprintbilltypeServices().QueryAsync(0);
                this.billtypeId.DataSource = typeList;
                this.billtypeId.DisplayMember = "name";
                this.billtypeId.ValueMember = "id";
                if (typeList.Count > 0) this.billtypeId.SelectedIndex = 0;
                #endregion
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void DoCreate()
        {
            try
            {
                if (new corecmsprintbilltemplateServices().InsertAsync(template))
                {
                    ShowSuccessTip("新增成功");
                    this.Close();
                }
                else
                {
                    UIMessageBox.Show("新增失败", "提示", UIStyle.Red, UIMessageBoxButtons.OK);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void DoEdit()
        {
            try
            {
                if (new corecmsprintbilltemplateServices().UpdateAsync(template))
                {
                    ShowSuccessTip("修改成功");
                    this.Close();
                }
                else
                {
                    UIMessageBox.Show("修改失败", "提示", UIStyle.Red, UIMessageBoxButtons.OK);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void btnOk_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (!CheckData()) return;
                template.billtypeId = CommFunc.ConvertToInt32(billtypeId.SelectedValue);
                template.name = this.name.Text;

                if (template.id > 0)
                {
                    DoEdit();
                }
                else
                {
                    DoCreate();
                }
                //SetControlClear();
            }
            catch (Exception)
            {
                throw;
            }
        }
        private bool CheckData()
        {
            try
            {
                if (billtypeId.Text == null || billtypeId.Text == "")
                {
                    UIMessageBox.Show("请输入模板格式", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
                    return false;
                }
                if (name.Text == null || name.Text == "")
                {
                    UIMessageBox.Show("请输入打印格式名称", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
                    return false;
                }
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void btnBillSetup_Click(object sender, EventArgs e)
        {

        }

        private void btnSetup_Click(object sender, EventArgs e)
        {
            corecmsprintbilltemplate printbilltemplate = new corecmsprintbilltemplateServices().SelectById(template.id);
            if (printbilltemplate == null)
            {
                UIMessageBox.Show("模板不存在，无法修改。", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }
            int sbilltypeId = CommFunc.ConvertToInt32(billtypeId.SelectedValue);
            int sfoodPrintType = printbilltemplate.foodPrintType;
            PrinterUnits.ShowPrintBillSet(null, printbilltemplate, 1, sbilltypeId, sfoodPrintType);

        }
    }
}
